<?php
/**
 * Description: 辅助方法
 * User: devforma
 * Date: 16/6/21
 * Time: 17:55
 */

class DailyPlanUtils {

    /**
     * 任务类型
     */
    const MISSIONTYPE_BEGINNER          = 1; //开启上岸计划
    const MISSIONTYPE_NOTELEARNING      = 2; //知识点学习
    const MISSIONTYPE_NOTEREVIEW        = 3; //知识点复习
    const MISSIONTYPE_CURRENTAFFAIR     = 4; //时政热点
    const MISSIONTYPE_NEWS              = 5; //新闻
    const MISSIONTYPE_NOTICE            = 6; //公告
    const MISSIONTYPE_EXAMSITUATION     = 7; //考情
    const MISSIONTYPE_TEST              = 8; //小测验
    const MISSIONTYPE_WEEKTEST          = 9; //周测验


    /**
     * 获取带缓存的上岸计划用户data
     *
     * @param int $userId 用户id
     * @return object
     */
    public static function getUserData($userId) {
        global $uddb;

        $cacheKey = 'dp:func:getUserData'.$userId;
        if (($dpUserData = cacheGet($cacheKey)) == null) {
            $dpUserData = $uddb->getRow('SELECT * FROM `dp_user_dailyplan_data` WHERE `user_id`=:uid', [':uid' => $userId]);

            if (empty($dpUserData))
                return false;

            cacheAdd($cacheKey, $dpUserData, 24*60*60);
        }

        return $dpUserData;
    }

    
    /**
     * 获取当前任务列表
     *
     * @param int $userId 用户id
     * @return array
     */
    public static function getCurrentMissions($userId) {
        global $uddb;

        //获取当天任务列表
        $sql = "SELECT * FROM `{$uddb->recentMissionInfo}` WHERE `user_id`=:uid AND `available`=1 ORDER BY `id` ASC";
        $missions = (array)$uddb->getResults($sql, [':uid' => $userId]);

        return $missions;
    }

    /**
     * 获取默认任务列表
     *
     * @param int $examDetailId 考试项目id
     * @param int $userId 注册用户id
     * @return array
     */
    public static function getDefaultMissionData($examDetailId = 1126, $userId = 0) {
        global $uddb;

        $defaultMissions = [];

        $examId = getExamByExamDetail($examDetailId);

        //获取默认任务
        $sql = 'SELECT * FROM `dp_default_mission_info` WHERE `exam_id`=:eid ORDER BY `id`';
        $originMissionData = (array)$uddb->getResults($sql, [':eid' => $examId]);

        if (!empty($originMissionData)) {
            foreach ($originMissionData as $item) {
                 $insertData = [
                     'user_id'            => $userId,
                     'mission_type'       => $item->mission_type,
                     'mission_title'      => $item->mission_title,
                     'mission_content_id' => $item->mission_content_id,
                     'mission_content'    => $item->mission_content,
                     'action_type'        => 0,
                     'mission_pair'       => $item->mission_pair,
                     'available'          => 1
                ];

                if ($userId) { //注册用户请求默认任务,需要写recent表
                    $uddb->insert($uddb->recentMissionInfo, $insertData);
                    $insertData['id'] = $uddb->lastInsertId();
                } else {
                    $insertData['id'] = (int)$item->id;
                }

                $defaultMissions[] = (object)$insertData;
            }

            //注册用户需生成测验任务
            if ($userId) {
                if (date('w') == 0) {
                    $missionType = DailyPlanUtils::MISSIONTYPE_WEEKTEST;
                    $missionTitle = '周测验';
                } else {
                    $missionType = DailyPlanUtils::MISSIONTYPE_TEST;
                    $missionTitle = '小测验';
                }

                $insertData = [
                    'user_id'            => $userId,
                    'mission_type'       => $missionType,
                    'mission_title'      => $missionTitle,
                    'mission_content_id' => 0,
                    'mission_content'    => '',
                    'mission_pair'       => 0,
                    'action_type'        => 0,
                    'available'          => 1
                ];

                $uddb->insert($uddb->recentMissionInfo, $insertData); //写表
                $insertData['id'] = $uddb->lastInsertId();

                $defaultMissions[] = (object)$insertData;
            }
        }

        return $defaultMissions;
    }


    /**
     * 获取任务类型说明
     *
     * @return array
     */
    public static function getMissionMeta() {
        global $uddb;

        $cacheKey = 'dp:func:getMissionMeta';
        $missionMeta = cacheGet($cacheKey);
        if (empty($missionMeta)) {
            $missionMeta = [];
            $taskTypeData = $uddb->getResults("SELECT * FROM `dp_conf_plan_type` WHERE 1");
            foreach ($taskTypeData as $type) {
                $missionMeta[$type->type_id] = $type;
            }
            cacheSet($cacheKey, $missionMeta, 3600*24);
        }

        return $missionMeta;
    }

    /**
     * 获取任务数据表记录
     *
     * @param int $missionId 任务id
     * @param bool $fromDefault 是否从默认表里取数据
     * @return null|object
     */
    public static function getMissionRecord($missionId, $fromDefault = false) {
        global $uddb;

        if ($fromDefault) {
            $mission = $uddb->getRow("SELECT * FROM `dp_default_mission_info` WHERE `id`=:task_id", [':task_id' => $missionId]);

            $mission->action_time = date('Y-m-d H:i:s');
            $mission->action_type = '0';

        } else {

            //先从recent表获取该任务记录
            $mission = $uddb->getRow("SELECT * FROM `{$uddb->recentMissionInfo}` WHERE `id`=:task_id", [':task_id' => $missionId]);

            //recent表无记录则到history表中去找
            if (empty($mission)) {
                $sql = "SELECT * FROM `dp_history_mission_action` WHERE `mission_id`=:task_id";
                $mission = $uddb->getRow($sql, [':task_id' => $missionId]);
                if (!empty($mission))
                    $mission->id = $mission->mission_id;
            }
        }

        return $mission;
    }
}